//
//  CZTabBarController.m
//  微博
//
//  Created by 小和尚 on 16/9/3.
//  Copyright © 2016年 小和尚. All rights reserved.
//

#import "CZTabBarController.h"

@interface CZTabBarController ()

@end

@implementation CZTabBarController

-(instancetype)init{

    if (self = [super init]) {
        
        DDLogDebug(@"%d %s",__LINE__,__func__);
    }

    return self;
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        DDLogDebug(@"%d %s",__LINE__,__func__);
        
        
        
        UIViewController * home = [self addControllerWithClass:[UIViewController class] title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
        
        UIViewController * message = [self addControllerWithClass:[UIViewController class] title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
        
        UIViewController * discover = [self addControllerWithClass:[UIViewController class] title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
        
        UIViewController * me =[self addControllerWithClass:[UIViewController class] title:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
        
        self.viewControllers = @[home,message,discover,me];
        
    }

    return self;
}

//抽取多余的代码
//1.把相同的代码放到一个方法中
//2.把不确定的参数抽取出来
//3.在合适的地方调用

-(UIViewController *)addControllerWithClass:(Class)class title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{
    
    //不是所有的控制器都是通过init来初始化的，所以不能写死
    
    UIViewController * vc = [[class alloc]init];
    
    return [self addControllerWithVc:vc title:title image:image selectedImage:selectedImage];
    
}
-(UIViewController *)addControllerWithVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{
    
    vc.tabBarItem.title = title;
    
    //设置标题选中的颜色
    
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    
    vc.tabBarItem.image = [UIImage imageNamed:image];
    
    //创建图片
    
    UIImage * newImage = [UIImage imageNamed:selectedImage];
    
    //让图片以原有图片样式显示
    
    newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //设置图片
    
    vc.tabBarItem.selectedImage = newImage;
    
    
    
    vc.view.backgroundColor = [UIColor greenColor];
    
    return vc;
}

@end
